package com.lepu.app.fun.index.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.config.UserConfig;
import com.app.utils.AppEnterTongJi;
import com.app.utils.Const;
import com.app.utils.LoginUtil;
import com.app.utils.MyNetUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.index.bean.BeanTool;
import com.lepu.app.fun.my.MyArticleInfoActivity;
import com.lepu.app.fun.tool.bbcsz.BbcsActivity;
import com.lepu.app.fun.tool.bbwy.BbwyMainActivity;
import com.lepu.app.fun.tool.bbxxjs.BbxxjsActivity;
import com.lepu.app.fun.tool.bmi.BmiActivity;
import com.lepu.app.fun.tool.cjsj.ChanJianActivity;
import com.lepu.app.fun.tool.dcb.DaiChanActivity;
import com.lepu.app.fun.tool.jqtx.JqtxMainActivity;
import com.lepu.app.fun.tool.jqtx.JqtxMainResultActivity;
import com.lepu.app.fun.tool.jqtx.util.ToolJqtxUtil;
import com.lepu.app.fun.tool.std.TaiDongActivity;
import com.lepu.app.fun.tool.szqx.GrowActivity;
import com.lepu.app.fun.tool.szqx.GrowAddActivity;
import com.lepu.app.fun.tool.tefypc.TaiErActivity;
import com.lepu.app.fun.tool.ycqjs.YcqjsActivity;
import com.lepu.app.fun.tool.ymsjb.YiMiaoActivity;
import com.lepu.app.fun.tool.ymsjb.YiMiaoAddActivity;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.app.main.activity.MainTabActivity;

public class IndexToolAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Context mContext = null;
    private List<BeanTool> mArrayList = null;

    public IndexToolAdapter(Context context, List<BeanTool> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (mArrayList != null && mArrayList.size() > 0) {
            size = mArrayList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.index_tool_grid_item, null);

            holder = new ViewHolder();

            TextView nameTextView = (TextView) convertView
                    .findViewById(R.id.nameTextView);
            holder.nameTextView = nameTextView;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BeanTool item = mArrayList.get(position);

        holder.nameTextView.setText(item.Title);
        Drawable topDrawable = mContext.getResources()
                .getDrawable(item.BgResId);
        topDrawable.setBounds(0, 0, topDrawable.getMinimumWidth(),
                topDrawable.getMinimumHeight());// 必须设置图片大小，否则不显示
        holder.nameTextView.setCompoundDrawables(null, topDrawable, null, null);

        holder.nameTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                BaseFragmentActivity activity = (BaseFragmentActivity) mContext;
                if (LoginUtil.jumpLogin(activity)) {
                    return;
                }

                Intent intent = null;
                int id = item.Id;
                switch (id) {
                    case BeanTool.TOOL_BY_BMI:
                        // bmi
                        AppEnterTongJi.clickEvent(36, "");

                        intent = new Intent(mContext, BmiActivity.class);
                        break;
                    case BeanTool.TOOL_BY_JQTX:
                        // 经期提醒
                        AppEnterTongJi.clickEvent(37, "");

                        if (ToolJqtxUtil.getInstance(MainTabActivity.getInstance())
                                .checkIfSetJqtx()) {
                            intent = new Intent(mContext,
                                    JqtxMainResultActivity.class);
                        } else {
                            intent = new Intent(mContext, JqtxMainActivity.class);
                        }

                        break;
                    case BeanTool.TOOL_BY_BBXXJS:
                        // 宝宝血型计算
                        AppEnterTongJi.clickEvent(77, "");

                        intent = new Intent(mContext, BbxxjsActivity.class);
                        break;
                    // ===
                    case BeanTool.TOOL_YQ_TEFYPC:
                        // 胎儿发育评测
                        AppEnterTongJi.clickEvent(38, "");

                        intent = new Intent(mContext, TaiErActivity.class);
                        break;
                    // 胎动
                    case BeanTool.TOOL_YQ_STD:
                        AppEnterTongJi.clickEvent(39, "");

                        intent = new Intent(mContext, TaiDongActivity.class);
                        break;
                    // 待产
                    case BeanTool.TOOL_YQ_DCB:
                        AppEnterTongJi.clickEvent(40, "");

                        intent = new Intent(mContext, DaiChanActivity.class);
                        break;
                    // 预产期自测
                    case BeanTool.TOOL_YQ_YCQZC:
                        AppEnterTongJi.clickEvent(78, "");

                        intent = new Intent(mContext, YcqjsActivity.class);
                        break;
                    // 产检时间表
                    case BeanTool.TOOL_YQ_CJSJB:
                        AppEnterTongJi.clickEvent(79, "");

                        intent = new Intent(mContext, ChanJianActivity.class);
                        break;
                    // 孕期食谱
                    case BeanTool.TOOL_YQ_SP:
                        AppEnterTongJi.clickEvent(83, "");

                        MyNetUtil.getInstance().requestToolYqspArticleDetail(
                                (BaseFragmentActivity) mContext);
                        break;
                    // ===
                    case BeanTool.TOOL_YZ_YZC:
                        // 月子餐
                        AppEnterTongJi.clickEvent(41, "");

//					intent = new Intent(mContext, MyArticleInfoActivity.class);
//					intent.putExtra("url", Setting.getIndexYzcUrl());

                        intent = new Intent(mContext, AppBroswerActivity.class);
                        intent.putExtra("url", Setting.getIndexYzcUrl());
                        intent.putExtra("need_share", true);
                        intent.putExtra("title", "月子餐");
                        intent.putExtra("cover", "");
                        break;
                    case BeanTool.TOOL_YZ_BBWYJL:
                        // 宝宝喂养记录
                        AppEnterTongJi.clickEvent(42, "");

                        intent = new Intent(mContext, BbwyMainActivity.class);
                        break;
                    // 生长曲线
                    case BeanTool.TOOL_YZ_SZQX:
                    case BeanTool.TOOL_YE_SZQX:

                        if (id == BeanTool.TOOL_YZ_SZQX) {
                            AppEnterTongJi.clickEvent(43, "");
                        } else {
                            AppEnterTongJi.clickEvent(45, "");
                        }

                        if (UserConfig.getConfigBoolean(mContext,
                                Const.CONFIG_APP_BABY_FIRST, true)) {
                            intent = new Intent(mContext, GrowAddActivity.class);
                            intent.putExtra("isFirst", true);
                        } else {
                            intent = new Intent(mContext, GrowActivity.class);
                        }

                        break;
                    // 疫苗时间表
                    case BeanTool.TOOL_YE_YMSJB:
                        AppEnterTongJi.clickEvent(44, "");

                        String date = UserConfig.getConfigString(mContext,
                                Const.CONFIG_YIMIAO_BABY_DATE, "");

                        if (TextUtils.isEmpty(date)) {
                            intent = new Intent(mContext, YiMiaoAddActivity.class);
                            intent.putExtra("isFirst", true);
                        } else {
                            intent = new Intent(mContext, YiMiaoActivity.class);
                        }

                        break;
                    case BeanTool.TOOL_YZ_BBCSZ:
                        // 宝宝出生证
                        AppEnterTongJi.clickEvent(80, "");

                        intent = new Intent(mContext, BbcsActivity.class);
                        break;
                    default:
                        break;
                }

                if (intent != null) {
                    activity.startActivity(intent, true);
                }
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        TextView nameTextView;
    }
}
